# Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base

  before_filter :authorize

  def authorize
    # user is logged in
    if session[:user]
        @activeProjects = Project.opened_projects;
        check_permissions;
        return;
    end	
    # user is not logged in and is requesting to login
    return if request.request_uri == '/user/login';
    # user is not logged in, redirect him to the login page
    redirect_to(:controller => 'user', :action => 'login');
  end

  def method_missing(methodname, *args)
   @methodname = methodname
   @args = args
   render 'root/404', :status => 404
  end

  def check_permissions
      if controller_name == 'user'
        if action_name == 'add' || action_name == 'edit'
            if session[:user].rights != 'ManageAll'
                flash[:error_msg] = 'Access denied';
		redirect_to(:controller => 'root', :action => 'home');
	    end
	end
	if action_name == 'projects'
            if session[:user].rights == 'ManageTasks'
                flash[:error_msg] = 'Access denied';
		redirect_to(:controller => 'root', :action => 'home');
	    end
	end
      end	
  end



  protected
  
  # get project
  # returns the project object based on project id

  def get_project(project_id = nil)
    unless (project_id) # maybe the project id doesn't exists in parameters
      flash[:error_msg] = "No project selected";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end
    unless (project_id =~ /^\d+$/) 
      flash[:error_msg] = "Invalid project id";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end
    project = Project.get_project(project_id); 
    unless (project) 
      flash[:error_msg] = "No project found";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end

    return project;
  end

  # get iteration
  # returns the interation object based on iteration id

  def get_iteration(iteration_id = nil)
    unless (iteration_id) # maybe the iteration id doesn't exists in parameters
      flash[:error_msg] = "No iteration selected";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end
    unless (iteration_id =~ /^\d+$/) 
      flash[:error_msg] = "Invalid iteration id";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end
    iteration = Iteration.get_iteration(iteration_id); 
    unless (iteration) 
      flash[:error_msg] = "No iteration found";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end

    return iteration;
  end


  # get task
  # returns the task object based on task id

  def get_task(task_id = nil)
    unless (task_id) # maybe the task id doesn't exists in parameters
      flash[:error_msg] = "No task selected";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end
    unless (task_id =~ /^\d+$/) 
      flash[:error_msg] = "Invalid task id";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end
    task = Task.get_task(task_id); 
    unless (task) 
      flash[:error_msg] = "No task found";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end

    return task;
  end

  
  # get comment
  # returns the comment object based on comment id

  def get_comment(comment_id = nil)
    unless (comment_id) # maybe the comment id doesn't exists in parameters
      flash[:error_msg] = "No comment selected";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end
    unless (comment_id =~ /^\d+$/) 
      flash[:error_msg] = "Invalid comment id";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end
    comment = Comment.get_comment(comment_id); 
    unless (comment) 
      flash[:error_msg] = "No comment found";
      redirect_to(:controller => 'project', :action => 'list');
      return;
    end

    return comment;
  end

end
